import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:leader_help/page/tab_controller/controller.dart';
import 'package:leader_help/utils/ifly/ifly_util.dart';
import 'package:leader_help/utils/util.dart';
import 'package:leader_help/utils/ifly/xf_api_utils.dart';

import 'index.dart';

class DemandSubmitController extends GetxController {
  DemandSubmitController();

  final state = DemandSubmitState();
  final mainCtrl = Get.find<TbController>();
  // tap
  void handleTap(int index) {
    Get.snackbar(
      "标题",
      "消息",
    );
  }

  void start() async {
    // await XFApiUtils().start((String result) {
    //   String str;
    //   int off = 0;
    //   if (state.textCtrl.text.isEmpty) {
    //     // state.textCtrl.text = result;
    //     str = result;
    //   } else {
    //     var _off = state.textCtrl.selection.baseOffset;
    //     off = _off;
    //     var _text = state.textCtrl.text;
    //     _text = _text.replaceRange(_off, _off, result);
    //     // state.textCtrl.text = _text;
    //     str = _text;
    //   }
    //   state.textCtrl.value = TextEditingValue(
    //       text: str,
    //       selection: TextSelection.fromPosition(TextPosition(
    //           affinity: TextAffinity.downstream, offset: off + result.length)));

    //   update();
    // });
    await ifly_audio(textCtrl: state.textCtrl);
    update();
  }

  void actionDemand() {
    start();
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
    AudioTextUtil().connect();
    mainCtrl.state.isShowFloat = false;
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
    AudioTextUtil().close();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
